package com.curry.android.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.curry.android.R;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import static java.lang.String.*;

/**
 * Intent帮助类
 * <p>
 * Create by cfr on unknown.Modify by wgf on 160603.
 *
 * @author 陈福荣
 */
public class IntentHelper<T> {

    private static IntentHelper intentHelper;

    private Map<String, Object> map = new HashMap<>();

    public static IntentHelper getInstances() {
        if (intentHelper == null) {
            intentHelper = new IntentHelper();
        }
        return intentHelper;
    }

    public void getIntent(Context context, Class s) {
        intentWithExtra(context, s, null, null, -1);
    }

    public void intentWithExtraString(Context context, Class<?> s,
                                      String name, T value) {
        intentWithExtra(context, s, name, value, -1);
    }

    public void intentWithExtra(Context context, Class<?> s,
                                String name, T value) {
        intentWithExtra(context, s, name, value, -1);
    }

    public void intentWithExtra(Context context, Class<?> s,
                                String name, T value, int flag) {
        Intent intent = new Intent(context, s);
        if (flag != -1) {
            intent.setFlags(flag);
        }
        if (name != null && !name.equals("")) {
            if (value instanceof Boolean) {
                intent.putExtra(name, (Boolean) value);
            } else if (value instanceof String) {
                intent.putExtra(name, (String) value);
            } else if (value instanceof Integer) {
                intent.putExtra(name, (Integer) value);
            } else if (value instanceof Map) {
                intent.putExtra(name, (Serializable) value);
            }
            map.put(name, value);
        }
        context.startActivity(intent);
        jumpActivityAnimation(context, false);
    }

    public void bundleWithExtra(Context context, Class<?> s, Serializable serializable) {
        bundleWithExtra(context, s, null, serializable, -1, -1);
    }

    public void bundleWithExtra(Context context, Class<?> s, Bundle bundle) {
        bundleWithExtra(context, s, bundle, null, -1, -1);
    }

    public void bundleWithExtra(Context context, Class<?> s, Bundle bundle, Serializable serializable) {
        bundleWithExtra(context, s, bundle, serializable, -1, -1);
    }

    public void bundleWithExtra(Context context, Class<?> s, Bundle bundle, Serializable serializable, int flag, int requestCode) {
        Intent intent = new Intent(context, s);
        if (serializable != null) {
            if (bundle == null) {
                bundle = new Bundle();
            }
            bundle.putSerializable("map", serializable);
        }
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        if (flag != -1) {
            intent.setFlags(flag);
        }
        if (requestCode != -1 && context instanceof Activity) {
            ((Activity) context).startActivityForResult(intent, requestCode);
        } else {
            context.startActivity(intent);
        }
        jumpActivityAnimation(context, false);
    }

    public Serializable getSerializable(Activity activity) {
        return activity.getIntent().getExtras().getSerializable("map");
    }


    public void bundleWithBundleForResult(Activity context,
                                          Class<?> s, Bundle bundle, int requestCode) {
        bundleWithExtra(context, s, bundle, null, -1, requestCode);
    }

    public Object[] getIntentWithExtraString(Activity activity, String... ff) {
        Object[] keys = null;
        for (int i = 0; i < ff.length; i++) {
            if (map.get(ff[i]) instanceof Boolean) {
                keys[i] = activity.getIntent().getBooleanExtra(ff[i], false);
            } else if (map.get(ff[i]) instanceof String) {
                keys[i] = activity.getIntent().getStringExtra(ff[i]);
            } else if (map.get(ff[i]) instanceof Integer) {
                keys[i] = activity.getIntent().getIntExtra(ff[i], 0);
            }
        }
        return keys;
    }

    public void jumpActivityAnimation(Context context,
                                      boolean isFinishActivity) {
        if (!(context instanceof Activity)) {
            return;
        }
        if (!isFinishActivity) {
            ((Activity) context).overridePendingTransition(
                    R.anim.anim_activity_enter, R.anim.anim_activity_exit);
        } else {
            ((Activity) context).overridePendingTransition(
                    R.anim.anim_activity_finish_exit,
                    R.anim.anim_activity_finish_enter);
        }
    }
}
